package com.yytek.nmp.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.nmp.domain.IotModelDefine;
import com.yytek.nmp.service.IIotModelDefineService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 物模型定义Controller
 * 
 * @author yytek-iot
 * @date 2022-07-05
 */
@Api(value = "物模型定义控制器", tags = {"物模型定义管理"})
@RestController
@RequestMapping("/define")
public class IotModelDefineController extends BaseController
{
    @Autowired
    private IIotModelDefineService iotModelDefineService;

    /**
     * 查询设备绑定物模型信息
     */
    @ApiOperation("查询设备绑定物模型信息")
    @GetMapping("/getDeviceModel")
    public AjaxResult getDeviceModel(String iotId)
    {
        IotModelDefine modelDefine = iotModelDefineService.getDeviceModelByIotId(iotId);
        return AjaxResult.success(modelDefine);
    }

    /**
     * 查询物模型定义列表
     */
    @ApiOperation("查询物模型定义列表")
    @RequiresPermissions("nmp:define:list")
    @GetMapping("/list")
    public TableDataInfo list(IotModelDefine iotModelDefine)
    {
        startPage();
        List<IotModelDefine> list = iotModelDefineService.selectIotModelDefineList(iotModelDefine);
        return getDataTable(list);
    }

    /**
     * 导出物模型定义列表
     */
    @ApiOperation("导出物模型定义列表")
    @RequiresPermissions("nmp:define:export")
    @Log(title = "物模型定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotModelDefine iotModelDefine)
    {
        List<IotModelDefine> list = iotModelDefineService.selectIotModelDefineList(iotModelDefine);
        ExcelUtil<IotModelDefine> util = new ExcelUtil<IotModelDefine>(IotModelDefine.class);
        util.exportExcel(response, list, "物模型定义数据");
    }

    /**
     * 获取物模型定义详细信息
     */
    @ApiOperation("获取物模型定义详细信息")
    @RequiresPermissions("nmp:define:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iotModelDefineService.selectIotModelDefineById(id));
    }

    /**
     * 新增物模型定义
     */
    @ApiOperation("新增物模型定义")
    @RequiresPermissions("nmp:define:add")
    @Log(title = "物模型定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody IotModelDefine iotModelDefine)
    {
        //判断名称是否重复
        if(iotModelDefineService.exeitByName(iotModelDefine.getModelName())){
            return AjaxResult.error("名称重复");
        }else{
            return toAjax(iotModelDefineService.insertIotModelDefine(iotModelDefine));
        }

    }

    /**
     * 修改物模型定义
     */
    @ApiOperation("修改物模型定义")
    @RequiresPermissions("nmp:define:edit")
    @Log(title = "物模型定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotModelDefine iotModelDefine)
    {
        //判断名称是否重复
        if(iotModelDefineService.exeitByNameAndNoId(iotModelDefine.getModelName(),iotModelDefine.getId())){
            return AjaxResult.error("名称重复");
        }else{
            return toAjax(iotModelDefineService.updateIotModelDefine(iotModelDefine));
        }
    }

    /**
     * 删除物模型定义
     */
    @ApiOperation("删除物模型定义")
    @RequiresPermissions("nmp:define:remove")
    @Log(title = "物模型定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotModelDefineService.deleteIotModelDefineByIds(ids));
    }
}
